<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use think\facade\Db;
use lizhili\Time;

class Update extends BaseController
{
    public function ajax()
    {
        $data=input('post.');
        if (!isset($data["lizhili"]) or !isset($data["type"]) or $data["lizhili"]!= "0d89b868429be6158ba1ebc0f7c073de") {
            return json(['code'=>400,'message'=>'错误']);
        }
        //修改头像
        if ($data['type']=='update_my_img') {
            $file = request()->file();
            if (isset($file['file'])) {
                try {
                    validate(['file'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'user_mini', $file['file']);
                    $key='/storage/'.strtr($info, " \ ", " / ");
                    Db::name('user')->where('token', $data['token'])->update(['avatarUrl'=>$key]);
                    return	json(['code'=>200,'data'=>$key]);
                } catch (\think\exception\ValidateException $e) {
                    return json(['code'=>400,'message'=>$e->getMessage()]);
                }
            } else {
                return json(['code'=>400,'message'=>'上传错误']);
            }
        }
        //默认上传文件
        if ($data['type']=='uploadCards') {
            $file = request()->file('');
            if (isset($file['file'])) {
                try {
                    validate(['file'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'file_mini', $file['file']);
                    $key='/storage/'.strtr($info, " \ ", " / ");
                    return	json(['code'=>200,'data'=>$key]);
                } catch (\think\exception\ValidateException $e) {
                    return json(['code'=>400,'message'=>$e->getMessage()]);
                }
            } else {
                return json(['code'=>400,'message'=>'上传错误']);
            }
        }
        //gui-upload-imageso 组件使用
        if ($data['type']=='upload_images') {
            $file = request()->file('');
            if (isset($file['img'])) {
                try {
                    validate(['img'=>'fileSize:5242880|fileExt:jpg,png,gif,webp,jpeg'])->check($file);
                    $info=\think\facade\Filesystem::disk('public')->putFile( 'img_mini', $file['img']);
                    $key='/storage/'.strtr($info, " \ ", " / ");
                    return	json(['code'=>200,'data'=>$key]);
                } catch (\think\exception\ValidateException $e) {
                    return json(['code'=>400,'message'=>$e->getMessage()]);
                }
            } else {
                return json(['code'=>400,'message'=>'上传错误']);
            }
        }
        return json(['code'=>400,'message'=>'非法获取']);
    }
}
